package com.nebula.salary.portal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nebula.salary.common.result.ServerResponse;
import com.nebula.salary.common.utils.SecurityUtil;
import com.nebula.salary.model.dto.PageDto;
import com.nebula.salary.model.pojo.Benefits;
import com.nebula.salary.model.vo.BenefitsVo;
import com.nebula.salary.model.vo.PageResult;
import com.nebula.salary.portal.mapper.BenefitsMapper;
import com.nebula.salary.portal.service.IBenefitsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * <p>
 * 节日福利表 服务实现类
 * </p>
 *
 * @author codex
 * @since 2022-01-24
 */
@Service
public class BenefitsServiceImpl extends ServiceImpl<BenefitsMapper, Benefits> implements IBenefitsService {
    
    /**
     * 根据年份分页查询节日福利
     *
     * @param year    年份
     * @param pageDto 分页查询数据传输类
     * @return 节日福利
     */
    @Override
    public ServerResponse<PageResult> selectBenefitsByPage(Integer year, PageDto pageDto) {
        Long userId = SecurityUtil.getLoginUser().getUser().getUserId();
        Page<BenefitsVo> page = new Page<>(pageDto.getPage(), pageDto.getLimit());
        IPage<BenefitsVo> list = baseMapper.selectBenefitsByPage(page, year, userId);
        List<BenefitsVo> benefitsVos = list.getRecords();
        assembleBenefitsVo(benefitsVos);
    
        // 当前页合计
        BigDecimal reduce = benefitsVos.stream().map(Benefits::getBenefitsSalary).reduce(BigDecimal.ZERO, BigDecimal::add);
    
        // 总计
        BigDecimal total = baseMapper.selectSumBenefitsByYear(userId, year);
    
        return getPageResultServerResponse(list, benefitsVos, reduce, total);
    }
    
    /**
     * 分页查询所有节日福利
     *
     * @param pageDto 分页查询数据传输类
     * @return 节日福利
     */
    @Override
    public ServerResponse<PageResult> selectAllByPage(PageDto pageDto) {
        Long userId = SecurityUtil.getLoginUser().getUser().getUserId();
        Page<BenefitsVo> page = new Page<>(pageDto.getPage(), pageDto.getLimit());
        IPage<BenefitsVo> list = baseMapper.selectAllByPage(page, userId, pageDto.isAsc() ? "asc" : "desc");
        List<BenefitsVo> benefitsVos = list.getRecords();
        assembleBenefitsVo(benefitsVos);
        
        // 当前页合计
        BigDecimal reduce = benefitsVos.stream().map(Benefits::getBenefitsSalary).reduce(BigDecimal.ZERO, BigDecimal::add);
        
        // 总计
        BigDecimal total = baseMapper.selectSumBenefits(userId);
    
        return getPageResultServerResponse(list, benefitsVos, reduce, total);
    }
    
    /**
     * 节日福利总计
     *
     * @return 节日福利总计
     */
    @Override
    public BigDecimal getSum() {
        return baseMapper.selectSumBenefits(SecurityUtil.getUserId());
    }
    
    /**
     * 节日福利年度统计
     *
     * @param userId 用户ID
     * @return 节日福利年度统计
     */
    @Override
    public List<Map<String, BigDecimal>> getBenefitsYearBar(Long userId) {
        return baseMapper.getBenefitsYearBar(userId);
    }
    
    /**
     * 组装节日福利前端显示类
     *
     * @param benefitsVos 节日福利前端显示类
     */
    private void assembleBenefitsVo(List<BenefitsVo> benefitsVos) {
        NumberFormat nf = NumberFormat.getInstance(Locale.CHINA);
        benefitsVos.forEach(benefitsVo -> {
            if (benefitsVo.getBenefitsSalary() != null) {
                benefitsVo.setBenefitsMicrometer(nf.format(benefitsVo.getBenefitsSalary()));
            } else {
                benefitsVo.setBenefitsMicrometer(nf.format(0));
            }
        });
    }
    
    private ServerResponse<PageResult> getPageResultServerResponse(IPage<BenefitsVo> list, List<BenefitsVo> benefitsVos, BigDecimal reduce, BigDecimal total) {
        NumberFormat nf = NumberFormat.getInstance(Locale.CHINA);
        Map<String, Object> map = new HashMap<>(3);
        map.put("result", benefitsVos);
        map.put("pageTotal", nf.format(reduce));
        map.put("total", nf.format(total));
        
        return ServerResponse.createBySuccess(new PageResult(list.getTotal(), benefitsVos.size(), map));
    }
}
